
<div class="layui-form hisi-search">
    <form action="{:url()}" method="get" id="hisi-table-search">
        <div class="layui-form-item">
            <label class="layui-form-label">门店</label>
            <div class="layui-input-inline">
                <select name="shop_id" class="layui-input field-shop_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {$shops|raw}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">会员</label>
            <div class="layui-input-inline">
                <select name="user_id" class="layui-input field-user_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {$users|raw}
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">员工</label>
            <div class="layui-input-inline">
                <select name="admin_id" class="layui-input field-admin_id" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  {$admins|raw}
                </select>
            </div>
        </div> 
        <div class="layui-form-item">
            <label class="layui-form-label">职位</label>
            <div class="layui-input-inline">
                <select name="admin_type" class="layui-input field-admin_type" lay-verify="required" lay-search="">
                  <option value="">直接选择或搜索选择</option>
                  <option value="顾问">顾问</option>
                  <option value="美容师">美容师</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">开始时间</label>
            <div class="layui-input-inline">
              <input type="text" class="layui-input field-start" id="start" name="start" placeholder="yyyy-MM-dd">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">结束时间</label>
            <div class="layui-input-inline">
              <input type="text" class="layui-input field-end" id="end" name="end" placeholder="yyyy-MM-dd">
            </div>
          </div>
        <div class="layui-form-item hisi-search-btn">
            <div class="layui-input-inline">
                <button type="submit" class="layui-btn layui-btn-normal layui-icon layui-icon-search">执行</button>
                <button type="button" class="layui-btn layui-btn-warm layui-icon layui-icon-download-circle export">导出</button>
            </div>
        </div>
    </form>
</div>
<table id="dataTable" lay-filter="test3"></table>
{include file="system@block/layui" /}
<script type="text/html" id="statusTpl">
    {{# if(d.id != 0){  }}
    <input type="checkbox" name="status" value="{{ d.status }}" lay-skin="switch" lay-filter="switchStatus" lay-text="正常|关闭" {{ d.status == 1 ? 'checked' : '' }} data-href="{:url('status')}?id={{ d.id }}">
    {{# } }}
</script>
<script type="text/html" title="操作按钮模板" id="buttonTpl">
    {{# if(d.id != 0){  }}
    <a href="{:url('edit')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-normal hisi-iframe-pop">修改</a>
    <a href="{:url('del')}?id={{ d.id }}" class="layui-btn layui-btn-xs layui-btn-danger j-tr-del">删除</a>

    {{# } }}
</script>
<script type="text/javascript">
    layui.use(['table','jquery','excel','laydate'], function() {
        var table = layui.table;
        var $ = layui.jquery;
        var laydate = layui.laydate;
        //日期范围
    laydate.render({
      elem: '#start'
    });
    laydate.render({
      elem: '#end'
    });
        table.render({
            elem: '#dataTable'
            ,url: '{:url('', input('get.'))}' //数据接口
            ,page: true //开启分页
            ,skin: 'row'
            ,even: true
            ,limit: 20
            ,text: {
                none : '暂无相关数据'
            } 
            ,toolbar: '#toolbar'
            ,defaultToolbar:['filter']
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'shop_name', title: '门店'}
                ,{field: 'sn', title: '订单编号'}
                ,{field: 'user_name', title: '会员'}
                ,{field: 'admin_name', title: '员工'}
                ,{field: 'admin_type', title: '职位'}
                ,{field: 'settlement', title: '结算方式'}
                ,{field: 'project', title: '项目名称'}
                ,{field: 'price1', title: '疗程卡充值'}
                ,{field: 'price2', title: '产品购买'}
                ,{field: 'price3', title: '会员卡充值'}
                ,{field: 'price4', title: '项目购买'}
                ,{field: 'num', title: '数量'}
                ,{field: 'price', title: '消费金额'}
                ,{field: 'proportion',edit: 'text', title: '提成比例',templet:function(d){
                    return d.proportion + '%'
                }}
                ,{field: 'proportion_price', title: '提成金额',templet:function(d){
                    return d.price * d.proportion / 100;
                }}
                ,{field: 'shougong', title: '手工'}
                ,{field: 'bak', title: '备注'}
                ,{field: 'createtime', width:160, title: '添加时间',sort:true}
            ]]
        });

          //监听单元格编辑
          table.on('edit(test3)', function(obj){
                var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
                $.ajax({
                    url:'{:url('setProportion')}',
                    data:{
                        id:data.id,
                        proportion:value
                    },
                    type:'post',
                    success:function(res){
                        layer.msg(res.message,{time:1000},function(){
                            if(res.status){
                                // window.location.href= window.location.href
                                var that = $('#hisi-table-search');
                                    arr = that.serializeArray(),
                                    where = new Array(),
                                    dataTable = that.attr('data-table') ? that.attr('data-table') : 'dataTable',
                                    options = new Function('return '+ that.attr('hisi-data'))() || {page: {curr:1}};

                                    for(var i in arr) {
                                        where[arr[i].name] = arr[i].value;
                                    }

                                options.url = that.attr('action');
                                options.where = where;

                                table.reload(dataTable, options);
                                return false;
                            }
                        });
                    }
                })

          });

          var excel = layui.excel;
          $(document).on('click','.export',function(){
              var form = $('#hisi-table-search').serializeArray();
              var where = new Array();
              form.push({name:'operation',value:2});
              $.ajax({
                 url: '{:url()}',
                 data:form,
                 dataType: 'json',
                 success: function(res) {
                     // 假如返回的 res.data 是需要导出的列表数据
                     console.log(res.data);// [{name: 'wang', age: 18, sex: '男'}, {name: 'layui', age: 3, sex: '女'}]
                     // 1. 数组头部新增表头
                     // 'ID','项目名称','数量','金额','手工','结算方式','职位','提成比例','添加时间','订单编号','会员','门店','员工','提成金额'
                     res.data.unshift({
                         id: '编号',
                         project:'项目名称',
                         num:'数量',
                         price:'金额',
                         price1:'疗程卡充值',
                         price2:'产品购买',
                         price3:'会员卡充值',
                         shougong:'手工',
                         settlement:'结算方式',
                         admin_type:'职位',
                         proportion:'提成比例',
                         createtime:'添加时间',
                         sn:'订单编号',
                         user_name:'会员',
                         shop_name:'门店',
                         admin_name:'员工',
                         proportion_price:'提成金额',
                         bak:'备注'
                         });
                     // 2. 如果需要调整顺序，请执行梳理函数
                     var data = excel.filterExportData(res.data, [
                         'id',
                         'project',
                         'num',
                         'price',
                         'price1',
                         'price2',
                         'price3',
                         'shougong',
                         'settlement',
                         'admin_type',
                         'proportion',
                         'createtime',
                         'sn',
                         'user_name',
                         'shop_name',
                         'admin_name',
                         'proportion_price',
                         'bak'
                     ]);
                     // 3. 执行导出函数，系统会弹出弹框
                     excel.exportExcel({
                         sheet1: data
                     }, '绩效.xlsx', 'xlsx');
                 }
             });
          })

    });
</script>
